package org.apache.mina.example.lily;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

public class SSL {

	public static void main(String[] args) {
		String protocol = "TLS";
		Socket socket = null;
		X509TrustManager x509 = new X509TrustManager() {

			@Override
			public X509Certificate[] getAcceptedIssuers() {
				return new X509Certificate[0];
			}

			@Override
			public void checkServerTrusted(X509Certificate[] chain,
					String authType) throws CertificateException {

			}

			@Override
			public void checkClientTrusted(X509Certificate[] chain,
					String authType) throws CertificateException {

			}
		};
		X509TrustManager[] tm = { x509 };
		SSLContext sslContext = null;
		SSLSocketFactory sslSocketFactory = null;
		try {
			
			sslContext = SSLContext.getInstance(protocol);
			sslContext.init(null, tm, new SecureRandom());
			sslSocketFactory = sslContext.getSocketFactory();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		}
		try {
			socket = sslSocketFactory.createSocket();
			socket.setReuseAddress(true);
			socket.setTcpNoDelay(true);
			socket.connect(new InetSocketAddress("mp.weixin.qq.com", 443));
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
//			String content = "GET https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN HTTP/1.1\r\n"
//					+ "Host: mp.weixin.qq.com\r\n"
//					+ "Connection: keep-alive\r\n"
//					+ "Cache-Control: max-age=0\r\n"
//					+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
//					+ "User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31\r\n"
//					+ "Accept-Encoding: gzip,deflate,sdch\r\n"
//					+ "Accept-Language: zh-CN,zh;q=0.8\r\n"
//					+ "Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n"
//					+ "Cookie: remember_acct=421549014@qq.com; ts_uid=1604313963; lv_irt_id=4e3a58a69275fcfc531bdfb33ff4e5be; pt2gguin=o0421549014; RK=75Z7Q/jOzA; pgv_pvi=8127024128; pgv_pvid=8748173385; o_cookie=421549014; cert=rByeuOUE3yxtzj4VrWIUbZ8faiJNXvEn\r\n\r\n";
			String content = 
				"POST https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN HTTP/1.1\r\n"+
				"Host: mp.weixin.qq.com\r\n"+
				"Connection: keep-alive\r\n"+
				"Content-Length: 80\r\n"+
				"Accept: application/json, text/javascript, */*; q=0.01\r\n"+
				"Origin: https://mp.weixin.qq.com\r\n"+
				"X-Requested-With: XMLHttpRequest\r\n"+
				"User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31\r\n"+
				"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n"+
				"Referer: https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN\r\n"+
				"Accept-Encoding: gzip,deflate,sdch\r\n"+
				"Accept-Language: zh-CN,zh;q=0.8\r\n"+
				"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n\r\n"+
				"username=421549014%40qq.com&pwd=cc37bb0193921ee3413f961ffde2f2b8&imgcode=&f=json\r\n\r\n";
			os.write(content.getBytes());
			int position = 0;
			byte[] data = new byte[4096];
			while((position=is.read(data,0,data.length))!=-1){
				System.out.println(new String(data,0,position,"UTF-8"));
			}
			is.close();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
